/**
 * Created by SCR on 2017/8/1.
 */
var NetworkModel = require('../model/Integrate/NetworkModel');
var NetworkCtrl = require('../control/Integrate/NetworkCtrl');
var MSAggreCtrl = require('../control/Integrate/MSAggreCtrl');

module.exports = function (app) {
    app.route('/integration/network/:ac')
        .get(function (req, res, next) {
            var ac = req.params.ac;
            if(ac == 'new'){
                return res.render('network/network',{
                    openli:'integration-li'
                });
            }
            else if(ac == 'query'){
                return res.render('network/networkList',{
                    openli:'integration-li'
                });
            }
            // TODO
            else if(ac == 'edit' || ac == 'detail'){
                var _id = req.query._id;
                NetworkModel.getByOID(_id,function (err, network) {
                    if(err){
                        return res.render('network/network',{
                            openli:'integration-li',
                            error: JSON.stringify(err)
                        });
                    }
                    else if(network && network != undefined){
                        if(ac == 'edit'){
                            return res.render('network/network',{
                                openli:'integration-li',
                                network:JSON.stringify(network)
                            });
                        }
                        else if(ac == 'detail'){
                            return res.render('network/network',{
                                openli:'integration-li',
                                network:JSON.stringify(network)
                            });
                        }
                    }
                    else{
                        return res.render('network/network',{
                            openli:'integration-li',
                            error: JSON.stringify(new Error('Can\'t find this network!'))
                        });
                    }
                })
            }
            else{
                next();
            }
        });

    app.route('/integration/network/delete')
        .delete(function (req, res, next) {
            var _id = req.body._id;
            NetworkModel.delete(_id,function (err, rst) {
                if(err){
                    return res.end(JSON.stringify({error:err}));
                }
                else{
                    return res.end(JSON.stringify({error:null,result:rst}));
                }
            });
        });

    app.route('/integration/network/save')
        .post(function (req, res, next) {
            var newNetwork = req.body;
            NetworkModel.save(newNetwork,function (err, rst) {
                if(err){
                    return res.end(JSON.stringify({error: err}));
                }
                else{
                    return res.end(JSON.stringify({error: null, result: rst}));
                }
            })
        });

    app.route('/integration/network/get')
        .get(function (req, res, next) {
            var _id = req.query._id;
            NetworkModel.getByOID(_id,function (err, network) {
                if(err){
                    return res.end(JSON.stringify({error:err}));
                }
                else{
                    return res.end(JSON.stringify({error:null,network:network}));
                }
            })
        });

    // region service operation
    app.route('/integration/network/getMS')
        .get(function (req, res, next) {
            var host = req.query.host;
            var port = req.query.port;
            NetworkCtrl.getMS(host,port,function (err, msList) {
                if(err){
                    return res.end(JSON.stringify({error:err}));
                }
                else{
                    return res.end(JSON.stringify({error:null,msList:msList}));
                }
            })
        });

    app.route('/integration/network/getMSDetailList')
        .get(function (req, res, next) {
            var mss = req.query.msList;
            NetworkCtrl.getMSDetailList(mss,function (err, rst) {
                if(err){
                    return res.end(JSON.stringify({error:err}));
                }
                else{
                    return res.end(JSON.stringify({error:null,MSListDetail:rst}));
                }
            })
        });

    // 容器需要
    app.route('/integration/network/getMSDetail')
        .get(function (req, res, next) {
            var _id = req.query._id;
            NetworkCtrl.getMSDetail(_id,function (err,rst) {
                err?res.end(JSON.stringify({error:err})):
                    res.end(JSON.stringify({error:null,MSDetail:rst}));
            })
        });

    app.route('/integration/network/getDS')
        .get(function (req, res, next) {
            var host = req.query.host;
            var port = req.query.port;
            NetworkCtrl.getDS(host,port,function (err, dsList) {
                if(err){
                    return res.end(JSON.stringify({error:err}));
                }
                else{
                    return res.end(JSON.stringify({error:null,dsList:dsList}));
                }
            })
        });

    // endregion
};